-- set_user_data_ini
--
local import = import
---@type behavior_ret
local bret = import('bt/bt_plugin/behavior3/behavior_ret')
---@type api
local api = import("api/api")
---@type configer
local configer = import("base/configer")
---@type redis
local redis = import("game/func/redis")
local M = {
    name = "set_user_data_ini",
    type = "Action",
    desc = "写入用户ini",
    args = {
        { name = "section", type = "string?", desc = "区块" },
        { name = "key", type = "string?", desc = "键" },
        { name = "str", type = "string?", desc = "值(string)" },
        { name = "add", type = "boolean?", desc = "开启累加计次" },
        {
            name = "nType",
            type = "enum?",
            desc = "过期时间",
            options = {
                {
                    name = "日",
                    value = 1,
                },
                {
                    name = "周",
                    value = 2,
                },
                {
                    name = "无",
                    value = 0,
                },
                {
                    name = "半日",
                    value = 3,
                },
            },
        },
    },
    input = { "区块?", "键?", "值?" },
    output = {},
}

function M.run(node, env, section, key, str)
    -- node.args.str
    section = section or node.args.section
    key = key or node.args.key
    str = str or node.args.str
    local nType = node.args.nType or 0
    local add = node.args.add or false
    if add then
        local iniValue
        if nType == 0 then
            iniValue = configer.get_user_profile(section, key)
            if not iniValue or iniValue == "" then
                configer.set_user_profile(section, key, 1)
            else
                iniValue = tonumber(iniValue) + 1
                configer.set_user_profile(section, key, iniValue)
            end
        elseif nType == 1 then
            iniValue = redis.get_user_profile_today_four(section, key)
            if not iniValue or iniValue == "" then
                redis.set_user_profile_today_four(section, key, 1)
            else
                iniValue = tonumber(iniValue) + 1
                redis.set_user_profile_today_four(section, key, iniValue)
            end
        elseif nType == 2 then
            iniValue = redis.get_user_profile_to_monday(section, key)
            if not iniValue or iniValue == "" then
                redis.set_user_profile_today_four(section, key, 1)
            else
                iniValue = tonumber(iniValue) + 1
                redis.set_user_profile_today_four(section, key, iniValue)
            end
        elseif nType == 3 then
            local hour = tonumber(os.date("%H"))
            if hour < 12 then
                hour = hour + 24
            end
            hour = hour - 12
            iniValue = redis.get_user_profile_today_four(section, key, hour)
            if not iniValue or iniValue == "" then
                redis.set_user_profile_today_four(section, key, 1)
            else
                iniValue = tonumber(iniValue) + 1
                redis.set_user_profile_today_four(section, key, iniValue)
            end
        end
    else
        if nType == 0 then
            configer.set_user_profile(section, key, str)
        elseif nType == 1 then
            redis.set_user_profile_today_four(section, key, str)
        elseif nType == 2 then
            redis.set_user_profile_today_four(section, key, str)
        elseif nType == 3 then
            redis.set_user_profile_today_four(section, key, str)
        end
    end
    return bret.SUCCESS
end

return M
